﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows.Forms;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;

namespace Plane
{
    public partial class Register : Form
    {
        public Register()
        {
            InitializeComponent();
        }

        private bool isPasswordVisible = false;

        //密码的显示与隐藏
        private void button_show1_Click(object sender, EventArgs e)
        {
            isPasswordVisible = !isPasswordVisible;
            //true可见，false不可见
            textBox_password.PasswordChar = isPasswordVisible ? '\0' : '*';

            if (isPasswordVisible)
            {
                button_show1.Image = Plane.Properties.Resources.OIP_1__2__1__1_;
            }
            else
            {
                button_show1.Image = Plane.Properties.Resources.close;
            }
        }

        private bool isEnsurePasswordVisible = false;

        private void button_show2_Click(object sender, EventArgs e)
        {
            isEnsurePasswordVisible = !isEnsurePasswordVisible;
            //true可见，false不可见
            textBox_ensure_password.PasswordChar = isEnsurePasswordVisible ? '\0' : '*';

            if (isEnsurePasswordVisible)
            {
                button_show2.Image = Plane.Properties.Resources.OIP_1__2__1__1_;
            }
            else
            {
                button_show2.Image = Plane.Properties.Resources.close;
            }
        }

        private void button_return_Click(object sender, EventArgs e)
        {
            Login_Register login_Register = new Login_Register();

            login_Register.Show();

            this.Close();
        }

        //检验密码是否合格
        private void ValidatePassword()
        {
            string password = textBox_password.Text.Trim();

            // 密码非空检查
            if (string.IsNullOrEmpty(password))
            {
                MessageBox.Show("密码不能为空", "错误", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                textBox_password.Focus();
            }

            // 基础密码要求：8-20位，至少包含字母和数字
            string basicPattern = @"^(?=.*[A-Za-z])(?=.*\d)[A-Za-z\d]{8,20}$";

            // 强密码要求：8-20位，至少一个大写字母、一个小写字母和一个数字
            string strongPattern = @"^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)[a-zA-Z\d]{8,20}$";

            // 验证密码强度
            bool isBasic = Regex.IsMatch(password, basicPattern);
            bool isStrong = Regex.IsMatch(password, strongPattern);

            if (!isBasic)
            {
                // 不符合基础要求，提示并恢复默认颜色
                MessageBox.Show("密码必须为8-20位，且包含字母和数字", "密码格式错误",
                    MessageBoxButtons.OK, MessageBoxIcon.Warning);
                textBox_password.Clear();
                textBox_password.Focus();
            }
            else if (isStrong)
            {
                // 强密码：深绿色
                textBox_strength.Text = "强";
                textBox_strength.ForeColor = Color.FromArgb(0, 100, 0); // 深绿色
            }
            else
            {
                // 基础密码：淡绿色
                textBox_strength.Text = "弱";
                textBox_strength.ForeColor = Color.FromArgb(144, 238, 144); // 淡绿色
            }
        }


        //密码输入处按下回车键检查密码是否合格
        private void textBox_password_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                //阻止按键默认行为
                e.SuppressKeyPress = true;
                ValidatePassword();
            }
        }

        //光标离开的时候检查密码是否合格
        private void textBox_password_Leave(object sender, EventArgs e)
        {
            //如果进入到了确认密码环节那就直接验证
            if(ActiveControl==textBox_ensure_password)
            {
                ValidatePassword();
                return;
            }
            //如果当前活动的控件是显示密码按钮就不进行验证，因为用户可能密码还未输完只是想看看已有的密码
            if (ActiveControl == button_show1)
            {
                return;
            }
            ValidatePassword();
        }


        private void textBox_ensure_password_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                e.SuppressKeyPress = true;
                string password=textBox_password.Text.Trim();
                string newpassword=textBox_ensure_password.Text.Trim(); //再次输入的密码
                //如果再次输入的密码与一开始的密码不同
                if(newpassword!=password)
                {
                    MessageBox.Show("两次输入的密码不一致，请重新输入！", "密码验证错误",
                         MessageBoxButtons.OK, MessageBoxIcon.Warning);

                    // 清空确认密码框并聚焦
                    textBox_ensure_password.Clear();
                    textBox_ensure_password.Focus();
                }
                //密码验证正确
                else
                {
                    string username=textBox_username.Text.Trim();
                    bool isAdd=UserDbHelper.AddUser(username, password,0,0,10,0,"100","W","S","A","D","J",50);
                    //如果成功添加
                    if (isAdd)
                    {
                        MainWindow mainWindow = new MainWindow(username);
                        mainWindow.Show();
                        this.Close();
                    }
                    else
                    {
                        textBox_username.Clear();
                        textBox_password.Clear();
                        textBox_ensure_password.Clear();
                        textBox_strength.Clear();
                        textBox_username.Focus();
                    }
                }
            }
        }

        private void button_ensure_Click(object sender, EventArgs e)
        {
            string password = textBox_password.Text.Trim();
            string newpassword = textBox_ensure_password.Text.Trim(); //再次输入的密码
                                                                      //如果再次输入的密码与一开始的密码不同
            if (newpassword != password)
            {
                MessageBox.Show("两次输入的密码不一致，请重新输入！", "密码验证错误",
                     MessageBoxButtons.OK, MessageBoxIcon.Warning);

                // 清空确认密码框并聚焦
                textBox_ensure_password.Clear();
                textBox_ensure_password.Focus();
            }
            else
            {
                string username = textBox_username.Text.Trim();
                bool isAdd = UserDbHelper.AddUser(username, password, 0, 0, 10, 0, "100", "W", "S", "A", "D", "J", 50);
                //如果成功添加
                if (isAdd)
                {
                    MainWindow mainWindow = new MainWindow(username);
                    mainWindow.Show();
                    this.Close();
                }
                else
                {
                    textBox_username.Clear();
                    textBox_password.Clear();
                    textBox_ensure_password.Clear();
                    textBox_strength.Clear();
                    textBox_username.Focus();
                }
            }
        }
    }
}
